<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/8/18 17:04
 * Modified Time: 2019/8/18 17:04
 * Version: 2.0
 */

namespace App\Modules\Employee\Repositories;


use App\Models\MyVisitHistory;
use App\Modules\System\Criterias\GetMyVisitorListCriteria;
use App\Modules\System\Criterias\getVisitorListCriteria;
use Flower\Core\Repository\Eloquent\BaseRepository;

class MyVisitHistoryRepository extends BaseRepository
{
    public function model()
    {
        return MyVisitHistory::class;
    }

    //获取理发师某天昨天访客数
    public function getBarberDateVisitorNum($user_id,$store_id, $date)
    {

        return $this->model->where('user_id', $user_id)
            ->where('store_id', $store_id)
            ->whereDate('visit_time','=',$date)
            ->get()
            ->count();
    }

    //获取理发师某一日期转化率
    public function getBarberDateVisitorOrderNum($user_id,$store_id, $date)
    {
        //访客数
        $visitor_num = $this->getBarberDateVisitorNum($user_id,$store_id,$date);

        //访客下单数
        $order_num = $this->model->where('user_id', $user_id)
            ->whereDate('visit_time','=',$date)
            ->get()
            ->sum('is_order');

        if($visitor_num==0){
            $conversion_rate = 0;
        }else{
            $conversion_rate = ($order_num / $visitor_num);
        }

        return $conversion_rate;
    }



    /**
     * 访客画图的数据 店长端
     * @param $storeId ','分隔的字符串
     * @param $startDate
     * @param $endDate
     * @return array
     * @author Happy dong
     * @modifiedTime 2019-08-18 20:11
     */
    public function getBarberVisitorGraphData($user_id,$storeId, $startDate, $endDate)
    {
        if ($startDate == $endDate) {
            $list = $this->model
                ->selectRaw('SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 0 and DATE_FORMAT(visit_time,\'%H\') <= 3) THEN 1 ELSE 0 end) AS hour3,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 3 and DATE_FORMAT(visit_time,\'%H\')  <= 6) THEN 1 ELSE 0 end) AS hour6,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 6  and DATE_FORMAT(visit_time,\'%H\')  <= 9) THEN 1 ELSE 0 end) AS hour9,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 9  and DATE_FORMAT(visit_time,\'%H\')  <= 12) THEN 1 ELSE 0 end) AS hour12,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 12  and DATE_FORMAT(visit_time,\'%H\')  <= 15) THEN 1 ELSE 0 end) AS hour15,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 15  and DATE_FORMAT(visit_time,\'%H\')  <= 18) THEN 1 ELSE 0 end) AS hour18,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 18  and DATE_FORMAT(visit_time,\'%H\')  <= 21) THEN 1 ELSE 0 end) AS hour21,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 21  and DATE_FORMAT(visit_time,\'%H\')  <= 24) THEN 1 ELSE 0 end) AS hour24')
                ->where('user_id',$user_id)
                ->whereRaw('store_id in ('.$storeId.') and date(visit_time) ="' . $startDate . '"')->first();

        } else {
            $list = $this->model
                ->selectRaw('DATE_FORMAT(visit_time,"%m-%d") as times, count(id) as visitor_count')
                ->where('user_id',$user_id)
                ->where('visit_time','>=',$endDate)
                ->whereIn('store_id',[$storeId])
                ->groupBy('times')
                ->get();
        }
        return $list->toArray();
    }
    public function getList($user_id,$store_id, $startDate, $endDate,$isLimit=0,$pageSize=10)
    {

        if(!$isLimit){

//            $list = $this->model->with('member:id,name,face')
//                ->where('store_id', '=', $storeId)
//                ->whereDate('created_at', '>=', $endDate)
//                ->whereDate('created_at', '<=', $startDate)
//                ->orderByDesc('created_at');

            $list =  $this->pushCriteria(new GetMyVisitorListCriteria($user_id,$store_id,$startDate,$endDate,true))->get();
        }else{
            $pageSize = empty($pageSize) ? 10 : $pageSize;
            $list = $this->pushCriteria(new GetMyVisitorListCriteria($user_id,$store_id,$startDate,$endDate))->paginate($pageSize, ['*'], 'paginate');
        }
        return $list;
    }
}